Mfisch Hors ligne Membre Niveau: Confirmé
Points: 189
Défis: 0 Email | Message
Posté le 06/01/2012 17:58
[LuaFX] setmetatable does not work:
When I run this program:
local print = graydraw.print
class = function(prototype)
local derived = {}
local derivedMT = {
__index = prototype,
__call = function(proto, ...)
local instance = {}
local instanceMT = {
__index = derived,
__call = function()
return nil, "ERROR: Attempt to invoke an instance of a class"
end,
}
setmetatable(instance, instanceMT)
if instance.init then
instance:init(...)
end
return instance
end,
}
setmetatable(derived, derivedMT)
return derived
end
car = class()
function car: init(power, type, lights, etc)
self.power = power
self.type = type
self.lights = lights*2
self.options = etc
end
jeep = car(60, "4x4", 12, {airconditioning = false,trailerhook = true})
if jeep.trailerhook then
print("The jeep can have a trailer")
else
print("No trailer for you ")
end
I get this error:
[string 'experiment.lua']:22: Attempt to call global 'setmetatable', a nil value
Apparently setmetatable is not a luaFX function? Could we add it to luaFX?
Sorry, but I won't include it until july, because I programmed my functions assuming the user won't access metatables, then I have to look if there won't be issues if I include it.
There are many other ways to do the same thing.
Maybe you should start with an easier code.
----------------------------------
Purobaz En ligne Membre Niveau: Aucun
Points: 2141
Défis: 108 Email | Message
I don't understand completely the code you wrote,
but try with this idea:
Metatables are a sort of table associated to an objet. It is hide to the user. If the metatable is equal to nil, then the objet works like every other object. But if, for example, the case 'add' is equal to a function (and not nil), then when we add the objet with + to an other objet, it calls the function associated to add (and note the usual addition).
The simpler idea to implement it without setmetatable is to associate the objet to a metatable (not hide), and use a special function to add.
my_car = { ....everything you want .... ; metatable = metacar }
-- my_car is explicitely associate to metacar (it doesn't copy metacar: it is a sort of pointer on metacar)
function add (item1, item2)
if (item1.metatable and item1.metatable.add)
-- it tests if metatable exist and if it has an 'add' case
then item1.metatable.add (item1, item2 )
elseif (item2.metatable and item2.metatable.add)
then item2.metatable.add(item2, item1)
else item1 + item2
end
end
With this code, you do exactly as lua do when you make item1+item2.
-- uses these function instead of () and [] to access to the metatables
-- here we can use this function if item has only one argument. There is a way to extend it to any number of argument using varargs, but I don't know how to use them.
local function my_call (item, arg)
if (item.metatable and item.metatable.call)
then item.metatable.call(item,arg)
else item(arg)
end
end
local function my_index(item, i)
if (item.metatable and item.metatable.index)
then item.metatable.index(item,arg)
else item[ i ]
end
end
class = function(prototype)
local derived = {}
local derivedMT = {
index = prototype,
call = function(proto, ...)
local instance = {}
local instanceMT = {
index = derived,
call = function()
return nil, "ERROR: Attempt to invoke an instance of a class"
end,
}
instance.metatable = instanceMT
if instance.init then
instance:init(...)
end
return instance
end,
}
derived.metatable = derivedMT
return derived
end
car = class()
function car: init(power, type, lights, etc)
self.power = power
self.type = type
self.lights = lights*2
self.options = etc
end
jeep = car(60, "4x4", 12, {airconditioning = false,trailerhook = true})
if jeep.trailerhook then
print("The jeep can have a trailer")
else
print("No trailer for you" )
end
Planète-Casio est un site communautaire indépendant et n'est donc pas affilié à Casio | Toute reproduction de Planète-Casio, même partielle, est interdite
Les fichiers, programmes et publications postés sur Planète-Casio restent la propriété de leurs auteurs respectifs et peuvent être soumis à des copyrights
Merci de respecter le travail des autres ! | CASIO est une marque déposée par CASIO Computer Co., Ltd